//
//  ViewController.m
//  ipa_dumper491
//
//  Created by philip on 12/9/15.
//  Copyright © 2015 7659. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) NSArray* array; //of KYApplicationInfo
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.navigationItem.title = @"用户应用程序";
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:self action:@selector(doRefresh:) forControlEvents:UIControlEventValueChanged];
    
    [self doRefresh:self];
}

-(void)doRefresh:(id)sender{
    self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"刷新中"];
    
    dispatch_async(dispatch_get_main_queue(), ^(){
        self.array = [KYAppInfoManager installedAppDetailInfo];
        [self endRefreshing];
    });
}

- (void) endRefreshing
{
    [self.refreshControl endRefreshing];
    self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"];
    
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"KYApplicationInfoTableIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    else {
        while ([cell.contentView.subviews lastObject ]!=nil) {
            [(UIView*)[cell.contentView.subviews lastObject]removeFromSuperview];
        }
    }
    
    NSUInteger row = [indexPath row];
    
    cell.textLabel.text = [[self.array objectAtIndex:row] Name];
//    cell.detailTextLabel.text = [[self.array objectAtIndex:row] bundleIdentifier];
    
//    if (row % 2 == 0) {
//        cell.backgroundColor= [UIColor grayColor];
//    }else{
//        cell.backgroundColor= [UIColor clearColor];
//    }

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    KYAppInfoDetailViewContoller* detailVC = [[KYAppInfoDetailViewContoller alloc] init];
    
    
    
    UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    KYAppInfoDetailViewContoller *detailVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"appDetailInfo"];
    detailVC.detailInfo = [self.array objectAtIndex:[indexPath row]];
    
    [self.navigationController pushViewController:detailVC animated:YES];
}

@end
